<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="css/elementUI.css">
    <!-- 引入组件库 -->
    <script src="js/elementUI.js"></script>
</head>
<body>
<div id="app">
    <div style="width: 600px;margin: 0 auto;">
        <el-button type="info" @click="add()">添加</el-button>
        <el-table
                :data="tableData"
                style="width: 100%">
            <el-table-column
                    prop="date"
                    label="日期"
                    width="280">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="姓名"
                    width="280">
            </el-table-column>
            <el-table-column
                    prop="address"
                    label="地址">
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="180px">

                <template slot-scope="scope">
                    <el-button type="danger" @click="del(scope)">删除</el-button>
                    <el-button type="primary" @click="editShow(scope)">修改</el-button>
                </template>

            </el-table-column>
        </el-table>


        <el-dialog
                title="员工信息展示"
                :visible.sync="dialogVisible"
                width="30%"
               >
            <el-form label-width="80px">
                <el-form-item label="员工姓名">
                    <el-input v-model="emp.name" />
                </el-form-item>
                <el-form-item label="出生日期">
                    <el-input v-model="emp.date" />
                </el-form-item>
                <el-form-item label="住址">
                    <el-input v-model="emp.address" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancle()">取 消</el-button>
                <el-button type="primary" @click="edit()">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</div>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                emp:{
                  id:'',
                  date:'',
                  name:'',
                  address:''
                },
                tableData: [{
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                }, {
                    date: '2016-05-04',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1517 弄'
                }, {
                    date: '2016-05-01',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1519 弄'
                }, {
                    date: '2016-05-03',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1516 弄'
                }],
                dialogVisible: false,
            }
        },
        methods: {
            del(scope) {
                console.log(scope)
                this.tableData.splice(scope.$index, 1)
                this.$message.warning("删除数据成功！！！")
            },
            editShow(scope){
                console.log(scope)
                this.dialogVisible = true

                this.emp = {
                    id:scope.$index,
                    date: scope.row.date,
                    name: scope.row.name,
                    address: scope.row.address
                }
            },
            cancle(){
                this.dialogVisible = false
            },
            edit(){
                let flag = false
                for (i in this.tableData){
                    if(i == this.emp.id){
                        flag = true
                        this.tableData[i].name = this.emp.name
                        this.tableData[i].date = this.emp.date
                        this.tableData[i].address = this.emp.address
                    }
                }
                if(flag == true){
                    this.$message.success("修改数据成功！！！")
                }else{
                    this.tableData.push({date:this.emp.date,name:this.emp.name,address:this.emp.address})
                    this.emp.date = ''
                    this.emp.name =''
                    this.emp.address = ''
                    this.$message.success("添加数据成功！！！")
                }

                this.dialogVisible = false
            },
            add(){
                this.dialogVisible = true
            }

        }
    })
</script>
</body>
</html>
